package com.benyanyi.image_grid_lib;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collection;
import java.util.List;

/**
 * @author mylove
 * @date 2021/9/22
 * @email ben@yanyi.red
 * @overview
 */
public class ImageRecycler extends RecyclerView {

    private int mMaxLength = 0;//列表最大长度
    private boolean isAdd = false;//是否需要添加图片操作
    static Drawable mAddRes; //添加图片图标
    static Drawable mDefaultRes;//默认图片
    private int mMinShowSize = 0;//最小显示个数
    private boolean mShowDelete = false;//是否需要删除图片
    static Drawable mDeleteRes;//删除图标
    private int mDeletePadding = 0;//删除图标内边距
    private int mGravity = 0;//默认位于右边
    private int mSingleLineSize = 3;//单行显示个数
    private int mItemPadding = 0;//item内边距
    private int mItemRadius = 0;//圆角
    private ImagerAdapter mAdapter;

    public ImageRecycler(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ImageRecycler(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageRecycler);
        this.mMaxLength = ta.getInt(R.styleable.ImageRecycler_ir_max_show, 0);
        this.isAdd = ta.getBoolean(R.styleable.ImageRecycler_ir_is_add, false);
        mAddRes = ta.getDrawable(R.styleable.ImageRecycler_ir_add_res);
        if (mAddRes == null) {
            mAddRes = ContextCompat.getDrawable(this.getContext(), R.mipmap.benyanyi_add_photo);
        }
        mDefaultRes = ta.getDrawable(R.styleable.ImageRecycler_ir_default_res);
        if (mDefaultRes == null) {
            mDefaultRes = ContextCompat.getDrawable(this.getContext(), R.mipmap.benyanyi_image_default);
        }
        this.mMinShowSize = ta.getInt(R.styleable.ImageRecycler_ir_min_show_size, 0);
        this.mShowDelete = ta.getBoolean(R.styleable.ImageRecycler_ir_show_delete, false);
        mDeleteRes = ta.getDrawable(R.styleable.ImageRecycler_ir_delete_res);
        if (mDeleteRes == null) {
            mDeleteRes = ContextCompat.getDrawable(this.getContext(), R.mipmap.benyanyi_delete_photo);
        }
        this.mDeletePadding = ta.getDimensionPixelSize(R.styleable.ImageRecycler_ir_delete_padding, getResources().getDimensionPixelSize(R.dimen.dp5));
        this.mGravity = ta.getInt(R.styleable.ImageRecycler_ir_delete_gravity, 0);
        this.mSingleLineSize = ta.getInt(R.styleable.ImageRecycler_ir_single_line_size, 3);
        this.mItemPadding = ta.getDimensionPixelSize(R.styleable.ImageRecycler_ir_item_padding, 0);
        this.mItemRadius = ta.getDimensionPixelSize(R.styleable.ImageRecycler_ir_item_radius, 0);
        this.mAdapter = new ImagerAdapter(mMaxLength, isAdd, mMinShowSize, mShowDelete, mDeletePadding, mGravity, mItemRadius);
        GridLayoutManager layoutManager = new GridLayoutManager(this.getContext(), this.mSingleLineSize);
        setLayoutManager(layoutManager);
        addItemDecoration(new SpacingItemDecoration(this.mSingleLineSize, this.mItemPadding, false));
        super.setAdapter(this.mAdapter);
        ta.recycle();
    }

    public void setOnAddImgClick(OnAddImgClick onAddImgClick) {
        this.mAdapter.setOnAddImgClick(onAddImgClick);
    }

    public void setOnRemoveClick(OnRemoveClick onRemoveClick) {
        this.mAdapter.setOnRemoveClick(onRemoveClick);
    }

    @Override
    public void setLayoutManager(@Nullable LayoutManager layout) {
        if (layout instanceof GridLayoutManager) {
            super.setLayoutManager(layout);
        } else {
            super.setLayoutManager(new GridLayoutManager(this.getContext(), this.mSingleLineSize));
        }
    }

    @Override
    public void addItemDecoration(@NonNull ItemDecoration decor) {
        super.addItemDecoration(new SpacingItemDecoration(this.mSingleLineSize, this.mItemPadding, false));
    }

    public void setAdapter(@Nullable Adapter adapter) {

    }

    public void addData(@IntRange(from = 0L) int position, @NonNull String imgUrl) {
        this.mAdapter.addData(position, imgUrl);
    }

    public void addData(@NonNull String imgUrl) {
        this.mAdapter.addData(imgUrl);
    }

    public void remove(@IntRange(from = 0L) int position) {
        this.mAdapter.remove(position);
    }

    public void setData(@IntRange(from = 0L) int index, @NonNull String imgUrl) {
        this.mAdapter.setData(index, imgUrl);
    }

    public void addData(@IntRange(from = 0L) int position, @NonNull Collection<? extends String> imgUrls) {
        this.mAdapter.addData(position, imgUrls);
    }

    public void addData(@NonNull Collection<? extends String> imgUrls) {
        this.mAdapter.addData(imgUrls);
    }

    public void replaceData(@NonNull Collection<? extends String> imgUrls) {
        this.mAdapter.replaceData(imgUrls);
    }

    public List<String> getData() {
        return this.mAdapter.getData();
    }

    public interface OnAddImgClick {
        void onAddImgClick();
    }

    public interface OnRemoveClick {
        void onRemoveClick(int position);
    }
}
